<?php

namespace app\controllers;

use Yii;
use app\tools\Re;
use app\models\Banner;
use app\models\User;
use app\models\Good;
use app\models\GoodCate;

class HomeController extends BaseController
{
    public function actionIndex(){
      $banner_list = Banner::find()->where(['status' => 1])->select("cover,good_id")->orderBy("sort desc")->all();
      $good_cate_list = GoodCate::find()->where(['status' => 1])->orderBy("sort desc")->all();
      $good_cate_data = [];
      if ($good_cate_list) {
        foreach ($good_cate_list as $v) {
          $good_list = Good::find()
          ->where(['cate_id' => $v->id, 'status' => 1])
          ->orderBy("sort desc, created_at desc")
          ->limit(6)
          ->all();
          $good_data = [];
          if ($good_list) {
            foreach ($good_list as $v1) {
              $good_data[] = [
                'good_id' => $v1->id,
                'good_name' => $v1->good->name,
                'good_img' => $v1->good->img,
                'good_price' => $v1->good->price,
              ];
            }
          }
          $good_cate_data[] = [
            'id' => $v->id,
            'name' => $v->name,
            'cover' => $v->cover,
            'good_list' => $good_data,
          ];
        }
      }

      $data = [
        'banner_list' => $banner_list,
        'good_cate_list' => $good_cate_data
      ];
      return Re::data(['data' => $data]);
    }
}
